package gmmxEditor;

import java.awt.Color;
import java.io.File;

public class Utils {

	public final static String	jpeg			= "jpeg";
	public final static String	jpg				= "jpg";
	public final static String	gif				= "gif";
	public final static String	tiff			= "tiff";
	public final static String	tif				= "tif";
	public final static String	png				= "png";

	public final static String	colorMaskRGBA	= "%R%,%G%,%B%,%A%";

	/**
	 * Get the extension of a file.
	 */
	public static String getExtension(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');

		if ((i > 0) && (i < (s.length() - 1))) {
			ext = s.substring(i + 1).toLowerCase();
		}
		return ext;
	}

	/**
	 * Returns a string representing the color in the given mask
	 * 
	 * @param c
	 *            java.awt.Color
	 * @param mask
	 *            String
	 * @return String
	 */
	public static String serializeColor(Color c, String mask) {
		String ret = "";

		ret = mask.replace("%R%", c.getRed() + "").replace("%G%", c.getGreen() + "").replace("%B%", c.getBlue() + "").replace("%A%", c.getAlpha() + "");

		return ret;
	}

	/**
	 * Imports a color from the given string with the given mask
	 * 
	 * @param color
	 *            String
	 * @param mask
	 *            String
	 * @return java.awt.Color
	 */
	public static Color deSerializeColor(String color, String mask) {
		String separator = "";

		for (int i = 0; i < mask.length(); i++) {
			if (!Character.isLetterOrDigit(mask.charAt(i)) && (mask.charAt(i) != '%')) {
				separator = mask.charAt(i) + "";
				break;
			}
		}

		String[] maskElements = mask.split(separator);
		String[] elements = color.split(separator);

		int red = 0, green = 0, blue = 0, alpha = 0;

		for (int i = 0; i < maskElements.length; i++) {
			if (maskElements[i].equals("%R%")) {
				red = Integer.parseInt(elements[i]);
			} else if (maskElements[i].equals("%G%")) {
				green = Integer.parseInt(elements[i]);
			} else if (maskElements[i].equals("%B%")) {
				blue = Integer.parseInt(elements[i]);
			} else if (maskElements[i].equals("%A%")) {
				alpha = Integer.parseInt(elements[i]);
			}
		}

		return new Color(red, green, blue, alpha);
	}
}